package com.meshnetics.gb.stack.mac.type;

import com.meshnetics.gb.type.DataType;
import com.meshnetics.gb.type.DataValue;
import com.meshnetics.gb.type.impl.ByteUtils;
import com.meshnetics.gb.type.impl.custom.StructureDataType;
import com.meshnetics.gb.type.impl.zcl.TypeBitmap;
import com.meshnetics.gb.type.impl.zcl.TypeBool;
import com.meshnetics.gb.type.impl.zcl.TypeUInt;

import java.nio.ByteBuffer;

/**
 * WRITE DESCRIPTION HERE
 *
 * @author AKovylin
 */
public class TypePANDescriptor extends StructureDataType {
    public static final TypePANDescriptor TYPE = new TypePANDescriptor();

    TypePANDescriptor() {
        super("panDescriptor",
                new String[]{
                        "coordAddress",
                        "logicalChannel",
                        "SuperframeSpec",
                        "gtsPermit",
                        "linkQuality",
                        "timestamp",
                        "secAndACL",
                },
                new DataType[]{
                        TypeDataAddress.TYPE,
                        TypeUInt.UINT_8,
                        TypeSuperframeSpec.TYPE,
                        TypeBool.BOOLEAN,
                        TypeUInt.UINT_8,
                        TypeUInt.UINT_24,
                        TypeBitmap.BITMAP_8
                });
    }

    public DataValue fromObject(Object value) {
        throw new UnsupportedOperationException("not implemented yet");
        // todo reimplement
    }

    public DataValue deserialize(ByteBuffer bb) {
        ByteUtils.ensureNotNull(bb);
        ByteUtils.ensureCapacity(bb, 14); // 14 - min length
        DataAddress addr = (DataAddress) TypeDataAddress.TYPE.deserialize(bb);
        ByteUtils.ensureCapacity(bb, 9, 9 + addr.getDataSize());
        DataValue[] values = new DataValue[getFieldCount()];
        values[0] = addr;
        for (int i = 1; i < values.length; i++) {
            values[i] = getFieldType(i).deserialize(bb);
        }
        return new PANDescriptor(this, values);
    }
}
